<?php
namespace App\library\services;


use App\Models\Pink;
use App\Models\PinkConfig;
use phpDocumentor\Reflection\Types\Self_;

class PinkService extends BaseServices
{

    /**
     * Author:Yingbo Sun
     * 团购看团列表
     * @param $province
     * @param $uid
     * @return array
     */
    public static function pinkList($province,$uid)
    {
        if (empty($province || empty($uid))){
            return self::fail('参数错误');
        }
        $day = date('Y-m-d H:i:s');
        $where[] = ['site_province' ,'=', $province];
        $where[] = ['start_time' ,'<', $day];
        $where[] = ['end_time' ,'>', $day];
        $where[] = ['status' ,'=', 1];
        $where[] = ['type' ,'=', 0];
        $res = PinkConfig::where($where)->orderBy('start_time','desc')
            ->with(['pink' => function ($query) use ($uid){
            $query->where(['uid'=>$uid])->select();
        }])->get()->toArray();
        if (!$res){
            return self::success('暂时没有团购');
        }
        //判断当前用户是否参团
        foreach ($res as &$v){
            if (!empty($v['pink'])){
                $v['type'] = 1;
            }else{
                $v['type'] = 0;
            }
        }
        unset($v);
        return self::success('获取成功',$res);
    }

    /**
     * Author:Yingbo Sun
     * 用户参团
     * @param $uid
     * @param $groupId
     * @return array
     */
    public static function joinPink($uid,$groupId,$province)
    {
        if (empty($uid) || empty($groupId) || empty($province)){
            return self::fail('参数错误');
        }
        $res = PinkConfig::where(['id'=>$groupId])->first();
        if (!empty($res)){
            $res = $res->toArray();
        }
        if (!$res['group_number'] > $res['participation_num']){
            return self::fail('参团人数已满');
        }
        $group = Pink::where(['group_id' => $groupId,'uid' => $uid])->first();
        if (empty($group)){
            $create = Pink::create(['group_id' => $groupId,'uid' => $uid]);
            if ($create){
                PinkConfig::where(['id' => $groupId])->increment('participation_num');
                $data = self::pinkList($province,$uid);
                return self::success('参团成功',$data);
            }else{
                return self::fail('参团失败');
            }
        }else{
            return self::fail('您已经参团了');
        }

    }

    /**
     * Author:Yingbo Sun
     * 经纪人修改状态
     * @param $uid
     * @param $groupId
     * @param $status
     * @return array
     */
    public static function agentUpdateStatus($uid,$groupId,$status)
    {
        if (empty($uid) || empty($groupId)){
            return self::fail('参数错误');
        }
        $res = PinkConfig::where(['broker_id' => $uid,'id' => $groupId])->first();
        if (empty($res)){
            return self::fail('参数错误');
        }
        $res->update(['status' => $status]);
        $data = $res->save();
        if ($data){
            return self::success('修改成功');
        }else{
            return self::fail('修改失败');
        }
    }

    /**
     * Author:Yingbo Sun
     * @param $province
     * @param $uid
     * @return array
     */
    public static function agentPinkList($province,$uid)
    {
        if (empty($uid) || empty($province)){
            return self::fail('参数错误');
        }
        $data = PinkConfig::where(['broker_id' => $uid,'site_province'=>$province])
            ->orderBy('created_at','desc')->get();
        if (empty($data)){
            $data = [];
        }else{
            $data = $data->toArray();
        }
        return self::success('获取成功',$data);
    }
}
